class MultiPlayerSocket {
    constructor (playground){
        this.playground = playground;

        this.ws = new WebSocket("wss://app1397.acapp.acwing.com.cn/wss/multiplayer/");
        this.start();
    }

    get_player(uuid){
        var players = this.playground.players;
        for (var i = 0 ; i < this.playground.players.length ; i++){
            if(uuid === players[i].uuid){
                return players[i];
            }
        }
        return null;
    }

    start(){
        this.receive();
    }
    
    receive(){
        var that = this
        this.ws.onmessage = function(e){
            var data = JSON.parse(e.data);
            var uuid = data.uuid
            if(uuid === that.uuid) return false;
            var event = data.event;
            if(event === "create_player" || event === "create player"){
                that.receive_create_player(uuid, data.username,data.photo);
            }else if(event === "move_to"){
                that.receive_move_to(uuid,data.tx,data.ty);
            }else if(event === "shoot_fireball"){
                that.receive_shoot_fireball(uuid,data.tx,data.ty,data.ball_uuid);
            }else if(event === "attack"){
                that.receive_attack(uuid,data.attackee_uuid,data.x ,data.y, data.angle, data.damage, data.ball_uuid);
            }else if(event === "blink"){
                that.receive_blink(uuid,data.tx,data.ty);
            }else if(event === "chat_add_message"){
                that.receive_chat_add_message(uuid,data.username,data.text);
            }else if (event === "vertigo"){
                that.receive_vertigo(uuid,data.attackee_uuid,data.x ,data.y,data.angle,data.time , data.damage ,data.ball_uuid);
            }else if(event === "shoot_vertigo"){
                that.receive_shoot_vertigo(uuid, data.tx,data.ty,data.ball_uuid);
            }
        }
    }

    send_blink(tx,ty){
        var that = this;
        this.ws.send(JSON.stringify({
            'event':'blink',
            'uuid':that.uuid,
            'tx':tx,
            'ty':ty,
        }));
    }

    receive_blink(uuid,tx,ty){
        var that = this;
        var player = this.get_player(uuid);
        if(player){
            player.blink(tx,ty);
        }
    }

    send_chat_add_message(username, text){
        var that = this;
        this.ws.send(JSON.stringify({
            'event':"chat_add_message",
            'uuid':that.uuid,
            'username':username,
            'text':text,
        }));
    }

    receive_chat_add_message(uuid,username,text){
        let player = this.get_player(uuid);
        player.playground.chat_field.add_message(username,text);
    }
    send_create_player(username , photo){
        var that = this;
        this.ws.send(JSON.stringify({
            'event':"create_player",
            'uuid':that.uuid,
            'username':username,
            'photo':photo
        }));
    }

    receive_create_player(uuid,username, photo){
        var player = new Player(
            this.playground,
            this.playground.width / 2 /this.playground.scale,
            0.5,
            0.05,
            "white",
            0.15,
            "enemy",
            username,
            photo);
        player.uuid = uuid;
        this.playground.players.push(player);
    }

    send_move_to(tx , ty){
        var that = this;
        this.ws.send(JSON.stringify({
            'event':"move_to",
            'uuid' : that.uuid,
            'tx':tx,
            'ty':ty
        }));
    }

    receive_move_to(uuid, tx, ty){
        let player = this.get_player(uuid);
        if(player){
            player.move_to(tx,ty);
        }
    }

    

    send_shoot_fireball(tx,ty, ball_uuid){
        let that = this;
        this.ws.send(JSON.stringify({
            'event':'shoot_fireball',
            'uuid':that.uuid,
            'tx':tx,
            'ty':ty,
            'ball_uuid':ball_uuid,
        }))
    }

    receive_shoot_fireball(uuid,tx,ty, ball_uuid){
        let player = this.get_player(uuid);

        if(player){
            let fireball = player.shoot_fireball(tx,ty);
            fireball.uuid = ball_uuid;
        }
    }

    send_attack(attackee_uuid , x , y , angle , damage , ball_uuid){
        let that = this;
        this.ws.send(JSON.stringify({
            'event':"attack",
            'uuid':that.uuid,
            'x':x,
            'y':y,
            'attackee_uuid':attackee_uuid,
            'angle':angle,
            'damage':damage,
            'ball_uuid':ball_uuid
        }))
    }

    send_vertigo(attackee_uuid , x , y ,angle , time , damage , ball_uuid){
        let that = this;
        this.ws.send(JSON.stringify({
            'event':"vertigo",
            'uuid':that.uuid,
            'x':x,
            'y':y,
            'attackee_uuid':attackee_uuid,
            'angle':angle,
            'time':time,
            'damage':damage,
            'ball_uuid':ball_uuid
        }))

    }

    receive_vertigo(uuid , attackee_uuid , x , y , angle , time , damage , ball_uuid){
        let attack = this.get_player(uuid);
        let attackee = this.get_player(attackee_uuid);

        if(attackee){
            attackee.receive_vertigo(x, y , angle ,time, damage , ball_uuid , attack);
        }
    }

    send_shoot_vertigo(tx,ty, ball_uuid){
        let that = this;
        this.ws.send(JSON.stringify({
            'event':'shoot_vertigo',
            'uuid':that.uuid,
            'tx':tx,
            'ty':ty,
            'ball_uuid':ball_uuid,
        }))
    }

    receive_shoot_vertigo(uuid,tx,ty, ball_uuid){
        let player = this.get_player(uuid);

        if(player){
            let vertigo = player.shoot_vertigo(tx,ty);
            vertigo.uuid = ball_uuid;
        }
    }
    receive_attack(uuid,attackee_uuid, x , y , angle ,damage , ball_uuid){
        let attack = this.get_player(uuid);       // 攻击者
        let attackee = this.get_player(attackee_uuid);    // 被攻击者
        
        if(attack && attackee){
            attackee.receive_attack(x ,y ,angle ,damage ,ball_uuid,attack);
        }
    }


}
